/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Product;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.ProductService;
import com.rzico.core.entity.SysMch;
import com.rzico.core.service.SysMchService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品档案控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-02-17
 */
@Api(description = "企业信息接口")
@RestController
@RequestMapping("/admin/enterprise")
public class EnterpriseAdminController extends BaseController {

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private ProductService productService;

    @Autowired
    private SysMchService sysMchService;

    /**
     * 获取当前企业资料
     *
     * @return
     */
    @ApiOperation("获取当前企业资料")
    @GetMapping("/getInfo")
    public CommResult<Enterprise> getInfo() {
        Enterprise result = enterpriseService.getCurrent();
        return CommResult.success(result);

    }

    /**
     * 初始化商家资料
     *
     * @return
     */
    @ApiOperation("初始化商家资料")
    @GetMapping("/initMch")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mchId", value = "商户 id", dataType = "Long", paramType = "query")
    })
    public CommResult<Enterprise> initMch(String mchId) {
        SysMch sysMch = sysMchService.selectByPrimaryKey(mchId);
        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise == null) {
            enterprise = enterpriseService.initEnterprise(sysMch);
        }
        return CommResult.success(enterprise);

    }


    /**
     * 更新产品档案
     *
     * @param enterprise
     * @return
     */
    @Log(desc = "更新产品档案", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新产品档案")
    @PostMapping("/update")
    public CommResult<Enterprise> update(@RequestBody Enterprise enterprise) {
        Assert.notNull(enterprise.getId(), "ID为空");
        Assert.notNull(enterprise.getMchId(), "mchId为空");
        Enterprise result = enterpriseService.update(enterprise);
        return CommResult.success(result);
    }


    /**
     * 一键装修
     *
     * @return
     */
    @Log(desc = "一键装修", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("一键装修")
    @PostMapping("/initISVData")
    public CommResult<Enterprise> initISVData(String mchId) {

        SysMch sysMch = sysMchService.selectByPrimaryKey(mchId);
        if (sysMch.getIsvId()==null) {
            return CommResult.error("不支持一键装修");
        }

        Enterprise enterprise = enterpriseService.findByMch(sysMch.getId());
        if (enterprise==null) {
            //没有登录过，需初始化企业
            enterprise = enterpriseService.initEnterprise(sysMch);
        }

        Map<String,Object> params = new HashMap<>();
        params.put("enterpriseId",enterprise.getId());
        Integer w = productService.selectRowCount(params);

        if (w>0) {
            return CommResult.error("已经上线不能装修");
        }

        enterpriseService.initISVData(enterprise);

        return CommResult.success("装修成功");

    }

}
